
package Generer;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;
import Webservices.DocumentServiceService;
import Webservices.FOPException_Exception;

/**
 * Servlet permettant la génération en PDF d'un fichier XML.
 * @author Valentin & David.
 */
@WebServlet(name = "GenererPDF", urlPatterns = {"/genererPDF"})
public class GenererPDF extends HttpServlet {
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/DocumentServiceService.wsdl")
    private DocumentServiceService service;
    
    
    /**
     * Fonction pour récupérer (POST) les paramètres de la page web.
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            PrintWriter out = response.getWriter();
            
            // On récupère l'identifiant du document que l'on souhaite retourner.
            String identifiant = request.getParameter("identifiantGenerer");
            byte[] xsl = Utils.Commons.convertirFile2Bytes(new File("generate_PDF.xsl"));
            byte[] resultat = generePDF(Integer.parseInt(identifiant), xsl);
            
            File fileRes = Utils.Commons.convertirBytes2File(resultat,"xml");
            
            response.setContentType("application/force-download");
            response.setContentLength((int)fileRes.length());
            //response.setContentLength(-1);
            response.setHeader("Content-Transfer-Encoding", "binary");
            response.setHeader("Content-Disposition","attachment; filename=\"" + "document.pdf");
            InputStream in = new FileInputStream(fileRes);
            BufferedInputStream bin = new BufferedInputStream(in);
            DataInputStream din = new DataInputStream(bin);
            
            while(din.available() > 0){
                out.print((char)din.read());
            }
            //Traitement...
            out.println("<h1>" + identifiant + "</h1>");
        } catch (FOPException_Exception ex) {
            Logger.getLogger(GenererPDF.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * Fonction du web service.
     * @param arg0 : identifiant du document XML.
     * @param arg1 : tableau de bytes pour la feuille XSL.
     * @return un tableau de bytes correspondant au PDF généré.
     * @throws FOPException_Exception
     */
    private byte[] generePDF(int arg0, byte[] arg1) throws FOPException_Exception {
        Webservices.DocumentService port = service.getDocumentServicePort();
        return port.generePDF(arg0, arg1);
    }
}
